﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Http;
using StackExchange.Profiling;
using StackExchange.Profiling.EntityFramework6;

namespace CanDoo.Oil.WebHost
{
    public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            // 在应用程序启动时运行的代码
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);

#if MINI//MiniProfiler
            InitProfilerSettings();
            MiniProfilerEF6.Initialize();
#endif

            //System.Diagnostics.Trace.Listeners.Clear();
#if DEBUG
            //            System.Diagnostics.Trace.AutoFlush = true;
            //            System.Diagnostics.Trace.Listeners.Add(new System.Diagnostics.TextWriterTraceListener(Server.MapPath(string.Format("~/App_Data/{0}.log", DateTime.Now.ToString("yyyyMMddhhss")))));
            //            System.Diagnostics.Trace.Listeners.Add(new System.Web.WebPageTraceListener());
#endif
        }

#if MINI//MiniProfiler
        void Application_BeginRequest()
        {
            if (Request.IsLocal)
            {
                MiniProfiler.Start();
            }
        }

        void Application_EndRequest()
        {
            MiniProfiler.Stop();
        }


        /// <summary>
        /// Customize aspects of the MiniProfiler.
        /// </summary>
        private void InitProfilerSettings()
        {
            // some things should never be seen
            var ignored = MiniProfiler.Settings.IgnoredPaths.ToList();
            ignored.Add("*.axd");
            ignored.Add("/res/");
            ignored.Add("/Scripts/");
            ignored.Add("/third-party/");
            ignored.Add("/Views/");
            MiniProfiler.Settings.IgnoredPaths = ignored.ToArray();
            //MiniProfiler.Settings.ExcludeAssembly("FineUIPro.dll");
            MiniProfiler.Settings.PopupRenderPosition = RenderPosition.BottomLeft;
            //MiniProfiler.Settings.Storage = new SampleWeb.Helpers.SqliteMiniProfilerStorage(SampleWeb.MvcApplication.ConnectionString);
            MiniProfiler.Settings.SqlFormatter = new StackExchange.Profiling.SqlFormatters.InlineFormatter();
        }
#endif

    }
}